import { defineStore } from "pinia";
import { ref, computed } from "vue";

// defineStore用于创建实例，第一个参数是模块名称
// 如果仓库用选项式来写，第二个参数是对象，否则的话是函数
// export const useCounterStore = defineStore("counter", {
//   state: () => {
//     return {
//       count: 3 as number,
//       name: "zhangsan" as string,
//     };
//   },
//   getters: {
//     doubleCount: (state) => state.count * 2,
//   },
//   actions: {
//     increment() {
//       setTimeout(() => {
//         this.count++;
//       }, 2000);
//     },
//   },
// });

export const useCounterStore = defineStore("counter", () => {
  const count = ref(3);
  const name = ref("zhangsan");

  const doubleCount = computed(() => {
    return count.value * 2;
  });

  const increment = () => {
    count.value++;
  };

  return { count, name, increment, doubleCount };
});
